package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.BusinessDao;
import cn.gson.boot.model.dao.ShenHeDao;
import cn.gson.boot.model.mapper.BusinessMapper;
import cn.gson.boot.model.pojos.Business;
import cn.gson.boot.model.pojos.Kehu;
import cn.gson.boot.model.pojos.Shenhe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class BusinessService {
    @Autowired
    BusinessDao businessDao;//出差dao

    @Autowired
    ShenHeDao shenHeDao;//审核dao

    @Autowired
    BusinessMapper businessMapper;//出差mapper

    //分页查询和模糊查询（查询用mapper）
    public List<Business> allbusines(String businessTheme,String businessState,
                                     String businessSqr,String businessPlace,String Start,String Over,String empName){
        String date_ = null;
        String date1_ = null;
        if(Start != null && !Start.equals("") && Over != null && !Over.equals("")){
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date_ = sdf.format(sdf.parse(Start));
                date1_ = sdf.format(sdf.parse(Over));
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
        System.err.println("开始"+date_);
        System.err.println("结束"+date1_);
        return businessMapper.allbusines(businessTheme,businessState,businessSqr, businessPlace,date_,date1_,empName);
    }

    //详情查询通过id
    public Business xqbusines(Integer bId){
        return businessMapper.xqbusines(bId);
    }

    //新增
    public void addbusiness(Business business){
        System.out.println("新增了");
        Date date = new  Date();
        Timestamp tt = new Timestamp(date.getTime());
        int id = business.getbId();//出差id
        System.out.println("客户编号："+id);
        if(id==0){
            business.setBusinessZtai(1);//删除状态
            business.setBusinessShenhe("待审核");
            business.setBusinessSqr(business.getBusinessSqr());
            Business save = businessDao.save(business);
            //新增审核表
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(save.getbId());
            shenhe.setShShouming("出差" );
            shenhe.setChuangJiantime(new Timestamp(new Date().getTime()));
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            shenhe.setShenqingPeople(save.getBusinessSqr());
            shenHeDao.save(shenhe);
        }else{
            System.out.println("修改了");
            Business b2 = businessDao.findById(id).get();
            b2.setBusinessSqr(business.getBusinessSqr());//申请人
            b2.setBusinessBzhu(business.getBusinessBzhu());//备注
            b2.setBusinessJk(business.getBusinessJk());//借款
            b2.setBusinessJtgj(business.getBusinessJtgj());//交通工具
            b2.setBusinessTxry(business.getBusinessTxry());//同行人员
            b2.setBusinessState(business.getBusinessState());//状态
            b2.setBusinessChufadi(business.getBusinessChufadi());//出发地
            b2.setBusinessPlace(business.getBusinessPlace());//目的地
            b2.setBusinessTheme(business.getBusinessTheme());//主题
            b2.setBusinessOver(business.getBusinessOver());//结束时间
            b2.setBusinessStart(business.getBusinessStart());//开始时间
            b2.setkId(business.getkId());//客户
            businessDao.save(b2);
        }
    }

    public void deleteBusiness(int id){
        Business b3 = businessDao.findById(id).get();
        b3.setBusinessZtai(0);
        businessDao.save(b3);
    }
}
